package com.zyz.common.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zyz.common.core.constants.Constants;
import com.zyz.common.core.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = ThreadLocalUtil.get(Constants.THREAD_LOCAL_USER_ID, Long.class);
        this.strictInsertFill(metaObject, "createBy",Long.class,userId);
        this.strictInsertFill(metaObject, "createTime",LocalDateTime.class, LocalDateTime.now());
//        this.strictInsertFill(metaObject, "updateTime",LocalDateTime.class, LocalDateTime.now());
//        this.strictInsertFill(metaObject, "updateBy",Long.class, 1L);
    }


    @Override
    public void updateFill(MetaObject metaObject) {
//        log.info("开始更新填充.........");
//        this.strictUpdateFill(metaObject, "updateTime",LocalDateTime.class, LocalDateTime.now());
//        this.strictUpdateFill(metaObject, "updateBy",Long.class, 1894975818952904705L);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateBy", ThreadLocalUtil.get(Constants.THREAD_LOCAL_USER_ID, Long.class), metaObject);
    }
}
